//
//  HX_CycleImage.m
//  06-Cyclelmages
//
//  Created by 韩旭 on 16/7/21.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HX_CycleImage.h"
NS_ASSUME_NONNULL_BEGIN
@interface HX_CycleImage ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *cycleScrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@end
NS_ASSUME_NONNULL_END
@implementation HX_CycleImage
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //     子控件初始化时frame不要与初始化方法的frame的一样
        //     推荐使用self，bounds / CGRectMake(0, 0, frame.size.width, frame.size.height)
        [self initCycleScrollView:frame];
        [self creatImagePages:frame];
    }
    return self;
}
#pragma mark - scrollView Delegate
// 实现协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //    _pageControl.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
    //    改变pageControl的对应下标
    NSInteger page = scrollView.contentOffset.x / self.bounds.size.width;
    _pageControl.currentPage = page - 1;
    if (page == 0) {
        //        改变滚动视图的偏移量
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width * (self.imageArray.count - 2), 0);
        _pageControl.currentPage = _pageControl.numberOfPages - 1;
    } else if (page == _imageArray.count - 1) {
        _cycleScrollView.contentOffset = CGPointMake( self.bounds.size.width, 0);
        _pageControl.currentPage = 0;
    }
}
- (void)initCycleScrollView:(CGRect)frame {
    _cycleScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _cycleScrollView.backgroundColor = [UIColor cyanColor];
    _cycleScrollView.showsHorizontalScrollIndicator = NO;
    _cycleScrollView.delegate = self;
    _cycleScrollView.pagingEnabled = YES;
    [self addSubview:_cycleScrollView];
}
//#warning  后期需要添加page的位置改变. 这里默认位置
- (void)creatImagePages:(CGRect)frame {
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width / 3, 20)];
    _pageControl.backgroundColor = [UIColor whiteColor];
    _pageControl.pageIndicatorTintColor = [UIColor redColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    _pageControl.center = CGPointMake(frame.size.width / 2, frame.size.height - 15);
    [_pageControl addTarget:self action:@selector(imagePageChange:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
}
- (void)imagePageChange:(UIPageControl *)page {
    [_cycleScrollView setContentOffset:CGPointMake((page.currentPage + 1) * self.bounds.size.width, 0) animated:YES];
    if (page.currentPage == 4) {
        _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
        page.currentPage = _cycleScrollView.contentOffset.x / self.bounds.size.width - 2;
    }
}
//自定义方法
#pragma mark - ImageArray;
- (void)setImagesArrayWithArray:(NSArray *)array {
    //   根据传过来的数组, 进行处理， 生成图片数组
    NSArray *imagesArray = [self handleArray:array];NSLog(@"%ld", imagesArray.count);
    //    逻辑判断是, 需要用到数组内容
    self.imageArray = imagesArray;
    //    根据数组生成对应的imageView
    [self createAllImageViewsWithImageArray:imagesArray];
    //    在做逻辑判断是， 需要用到数组
    //        直接使用数组创建ImageView
    
    _pageControl.numberOfPages = array.count;
    
    //    因为添加了最后一张图, 要让轮播图看起来在第一张
    _cycleScrollView.contentOffset = CGPointMake(self.bounds.size.width, 0);
    _cycleScrollView.contentSize = CGSizeMake(self.frame.size.width * (_pageControl.numberOfPages + 2), 0);
}
- (NSArray *)handleArray:(NSArray *)handArray {
    //    判断类型（不知道别人使用你类的时候传进来的类型是什么）
    id firstObject = [handArray firstObject];
    //    最终的图片数组
    NSMutableArray *arrImageArray = [NSMutableArray array];
    //    判断对象是不是某种类型isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        //    把 字符串数组转化成图片UIImage数组
        //        使用数组创建imageView
        for (NSString *imageName in handArray) {
            UIImage *image = [UIImage imageNamed:imageName];
            [arrImageArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        [arrImageArray setArray:handArray];
    }
    //    获取到最后第一张图
    UIImage *firstImage = [arrImageArray firstObject];
    //    获取到最后一张图
    UIImage *lastImage = [arrImageArray lastObject];
    //    首位下标为0的为值方最后一张图
    [arrImageArray insertObject:lastImage atIndex:0];
    //    在最后添加第一张图
    [arrImageArray addObject:firstImage];
    return [arrImageArray copy];
}
- (void)createAllImageViewsWithImageArray:(NSArray *)imagesArray {
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.bounds.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        imageView.image = imagesArray[i];
        [_cycleScrollView addSubview:imageView];
    }
}
@end
